package com.valuedaas.schoolsafety.common.base;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.bson.types.ObjectId;

public class Result {

	public enum Status {
		OK, ERROR
	}

	private static SerializeConfig config = new SerializeConfig();

	private String status;
	private String message;
	private Object result;
	private Integer code;

	static {
		config.put(ObjectId.class, ObjectIdSerializer.INSTANCE);
	}

	public boolean isOk() {
		return Status.OK.name().equals(status);
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Object getResult() {
		return result;
	}

	public void setResult(Object result) {
		this.result = result;
	}

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public Result(int code, String status, String message, Object result) {
		super();
		this.status = status;
		this.message = message;
		this.result = result;
		this.code = code;
	}

	public Result(int code, String status, String message) {
		super();
		this.status = status;
		this.message = message;
		this.code = code;
	}

	public Result() {
	}

	public static String error(Integer code, String msg, Object result) {
		return new Result(code, Status.ERROR.name(), msg, result).toString();
	}

	public static String error(Integer code, String msg) {
		return new Result(code, Status.ERROR.name(), msg, null).toString();
	}

	public static String error(String errMsg) {
		return new Result(1000, Status.ERROR.name(), errMsg, null).toString();
	}

	public static String error(String errMsg, Object result) {
		return new Result(1000, Status.ERROR.name(), errMsg, result).toString();
	}

	public static String ok(Integer code, String msg, Object result) {
		return new Result(code, Status.OK.name(), msg, result).toString();
	}

	public static String ok(String msg, Object result) {
		return new Result(0, Status.OK.name(), msg, result).toString();
	}

	public static String ok(Object result) {
		return new Result(0, Status.OK.name(), "", result).toString();
	}

	@Override
	public String toString() {
	
		return JSONObject.toJSONString(this, config, SerializerFeature.WriteMapNullValue,
				SerializerFeature.DisableCircularReferenceDetect,
				SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty);
	}

}
